package client.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;

import javax.swing.*;

import messages.*;

import sharedspace.*;

import client.gui.GUI_AbstractMainMenu.MainMenuType;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
 * This JPanel purpose is to show all statistics of all executed exams from the database
 * @author Amir S.
 * @author Jacob C.
 * @author Maya G.
 * @author Michael V.
 *  
 */

public class GUI_Manager_Statistics<trList> extends GUI_AbstractMainMenu{
	public JButton btnBack = null;
	private JScrollPane dataScroller = null;
	private JTable dataJTable = null;
	private List<StatisticTestInformation> sList = null;
	private List<Teacher> tList = null;
	private List<Student> stList = null;
	private List<Course> cList = null;
	public List<ExecuteTest> exList = null;
	public JComboBox<String> comboBoxOption = null;
	public JComboBox<String> comboBoxChoise = null;
	private JTable statisticJTable = null;

	public Client_GUI ms=null;
	
	public GUI_Manager_Statistics(Client_GUI ms) {
		super(MainMenuType.MANAGER,ms);
		this.ms=ms;
		initiate();
		greetMsg.setText("View Statistic Reports!");
		mainMenu.setText("Manager");
		

	}
	
	private void initiate()
	{		
		background.add(getbtnBack(),null);
		background.add(btnBack);
		background.add(getComobBoxOptions());
		background.add(getComobBoxChoise());
		
	}
	
	private JButton getbtnBack()
	{
		if (btnBack == null)
		{
			btnBack = new JButton("Back");	
			btnBack.setBounds(53, 28, 107, 30);
			btnBack.setVisible(true);
		}
		
		return btnBack;
	}
	
	private JComboBox<String> getComobBoxOptions()
	{
		int i;
		if (comboBoxOption == null)
		{
			comboBoxOption = new JComboBox();
			comboBoxOption.setSelectedIndex(-1);
			comboBoxOption.setEnabled(true);
			comboBoxOption.setBounds(150, 150, 150, 30);
			comboBoxOption.addItem("Choose");
			comboBoxOption.addItem("Teacher");
			comboBoxOption.addItem("Course");
			comboBoxOption.addItem("Student");
			comboBoxOption.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e1) {
						if (statisticJTable != null) {
							ms.mst.background.remove(getStatisticTable());
							ms.mst.statisticJTable = null;
							ms.mst.repaint();
						}
						ActionListener al = comboBoxChoise.getActionListeners()[0];
						switch (comboBoxOption.getSelectedIndex())
						{
						case 0:
							comboBoxChoise.removeActionListener(comboBoxChoise.getActionListeners()[0]);
							comboBoxChoise.setEnabled(false);
							if (statisticJTable != null) ms.mst.background.remove(getStatisticTable());
							ms.mst.repaint();
							comboBoxChoise.removeAllItems();
							comboBoxChoise.addActionListener(al);
							break;	
							
						case 1:
							comboBoxChoise.removeActionListener(comboBoxChoise.getActionListeners()[0]);
							comboBoxChoise.removeAllItems();
							comboBoxChoise.setEnabled(true);
							MessageTeacher MsgGtl = new MessageTeacher();
							try {
								ms.client.sendToServer(MsgGtl);
							} catch (IOException e) {
								e.printStackTrace();
							}
							MessageTeacherReply MsgRep;
							MsgRep = (MessageTeacherReply)ms.client.GetMessage();
							tList = MsgRep.getTeacherList();
							for (int i = 0; i<tList.size(); i++){
								comboBoxChoise.addItem(tList.get(i).getUserFirstName()+" "+tList.get(i).getUserLastName());
							}
							comboBoxChoise.addActionListener(al);
							break;
							
						case 2:
							comboBoxChoise.setEnabled(true);
							comboBoxChoise.removeActionListener(comboBoxChoise.getActionListeners()[0]);
							comboBoxChoise.removeAllItems();
							MessageAllCourses MsgGcl = new MessageAllCourses();
							try {
								ms.client.sendToServer(MsgGcl);
								cList = (ArrayList<Course>)ms.client.GetMessage();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							for (int i = 0; i<cList.size(); i++){
								comboBoxChoise.addItem(cList.get(i).getCourseName());
							}
							comboBoxChoise.addActionListener(al);
							break;
							
						case 3:
							comboBoxChoise.setEnabled(true);
							comboBoxChoise.removeActionListener(comboBoxChoise.getActionListeners()[0]);
							comboBoxChoise.removeAllItems();
							MessageAllStudents MsgGsl = new MessageAllStudents();
							try {
								ms.client.sendToServer(MsgGsl);
								stList = (ArrayList<Student>)ms.client.GetMessage();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							for (int i = 0; i<stList.size(); i++){
								comboBoxChoise.addItem(stList.get(i).getUserFirstName()+" "+stList.get(i).getUserLastName());
							}
							comboBoxChoise.addActionListener(al);
							break;
						}
					}

				});	
		} 
		return comboBoxOption;
	}
	
	private JComboBox<String> getComobBoxChoise()
	{
		int i;
		if (comboBoxChoise == null)
		{
			comboBoxChoise = new JComboBox();
			comboBoxChoise.setSelectedIndex(-1);
			comboBoxChoise.setEnabled(true);
			comboBoxChoise.setBounds(350, 150, 250, 30);
			comboBoxChoise.setEnabled(false);

		} 
		comboBoxChoise.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e1) {
				switch (comboBoxOption.getSelectedIndex())
				{
				case 0:
					break;							
				case 1:
					MessageExecutedExamListByTeacherID msgGel = new MessageExecutedExamListByTeacherID(tList.get(comboBoxChoise.getSelectedIndex()).getUserID());
					try {
						ms.client.sendToServer(msgGel);
						MessageExecutedExamListReply MsgGelRep = (MessageExecutedExamListReply)ms.client.GetMessage();
						exList = MsgGelRep.getExTestList();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					break;
				case 2:
					MessageExecutedExamListByCourseID MsgGcl = new MessageExecutedExamListByCourseID(cList.get(comboBoxChoise.getSelectedIndex()).getCourseID());
					try {
						ms.client.sendToServer(MsgGcl);
						MessageExecutedExamListReply MsgGslRep = (MessageExecutedExamListReply)ms.client.GetMessage();
						exList = MsgGslRep.getExTestList();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					break;
				case 3:
					MessageExecutedExamListByStudentID MsgGsl = new MessageExecutedExamListByStudentID(stList.get(comboBoxChoise.getSelectedIndex()).getUserID());
					try {
						ms.client.sendToServer(MsgGsl);
						MessageExecutedExamListReply MsgGslRep = (MessageExecutedExamListReply)ms.client.GetMessage();
						exList = MsgGslRep.getExTestList();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					break;
				}
				sList = new ArrayList<StatisticTestInformation>();
				for (int i=0; i<exList.size();i++){
					MessageStatisticInfoListByExecuteTestList MsgSibeti = new MessageStatisticInfoListByExecuteTestList(exList);
					try {
							ms.client.sendToServer(MsgSibeti);
							sList =(ArrayList<StatisticTestInformation>)ms.client.GetMessage();
						} 
					catch (IOException e) {
							e.printStackTrace();
						}

				}
				ms.mst.background.remove(getStatisticTable());
				ms.mst.statisticJTable = null;
				ms.mst.background.add(getStatisticTable());
				ms.mst.repaint();
			}

		});	
		return comboBoxChoise;
	}

	private JScrollPane getStatisticTable()
	{
		if (statisticJTable == null)
		{
			String columnNames[] = {"ID","Avarage","Median","0-10","10-20","20-30", "30-40", "40-50", "50-60", "60-70", "70-80", "80-90", "90-100", "Student Number"};
			String tableModel[][] = new String[sList.size()][StatisticTestInformation.NUM_OF_COLUMNS];
			for (int i=0; i<sList.size(); i++){
				tableModel[i][0] = sList.get(i).getExecuteTestID();
				tableModel[i][1] = ""+sList.get(i).getTestAvarage();
				tableModel[i][2] = ""+sList.get(i).getTestMedian();
				for (int j = 3; j<13 ; j++)
					tableModel[i][j] = "" + sList.get(i).getGradeDistribution()[j-3];
				tableModel[i][13] = ""+sList.get(i).getCount();
			}
			statisticJTable = new JTable(tableModel, columnNames){
				   public boolean isCellEditable(int rowIndex, int colIndex) {
				   return false; //Disallow the editing of any cell
				   }
				   };		 
				   statisticJTable.setFont(new Font("Tahoma", Font.PLAIN, 14));
				   statisticJTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
				   statisticJTable.getTableHeader().setReorderingAllowed(false);
				   statisticJTable.setEnabled(false);
			dataScroller= new JScrollPane(statisticJTable);
			dataScroller.setBounds(50, 250, 900, 400);
		}
			
		
		return dataScroller;
	}

}
